<?php

namespace app\apisell\controller\admin;

use app\apisell\service\RegisterLink\RegisterLinkService;
use app\BaseController;
use app\common\controller\QywxController;
use app\common\miniProgram\MiniProgram;
use app\common\utils\Res;
use think\facade\Db;

class RegisterLink extends BaseController
{
    //注册链接列表
    public function getList()
    {
        $param = $this->request->param();
        $param['is_popu'] =  $this->request->isPopu;
        $result = RegisterLinkService::getList($param);
        return Res::ok($result);
    }
    
    //创建注册链接
    public function createRegisterLink()
    {
        if(!$this->checkPermission()){
            return Res::error('你没有权限创建注册链接，请联系管理员！');
        }
        $param = $this->request->param();
        $param['add_id'] =  $this->request->popuId;
        $param['link_level'] = 1;
        $result = RegisterLinkService::createRegisterLink($param);
        return Res::ok($result);
    }
    
    //创建一级推广员链接
    public function createOneRegisterLink()
    {
        $link_path = env('register.register_host');
        $param = 'id=pingtai';
        $reg_link_qrcode = MiniProgram::createMiniProgramQrCodeUnlimit($link_path,$param);
        $result = [
            'qrcode' => $reg_link_qrcode
        ];
        return Res::ok($result);
    }
    
    //获取员工列表
    public function getStaffList()
    {
        $result = RegisterLinkService::getStaffList();
        return Res::ok($result);
    }
    
    //获取自身信息
    public function getSelfInfo()
    {
        $uid = $this->request->uid;
        $result = Db::table('xh_sell_popu_user')
            ->where('member_id',$uid)
            ->field('id,popu_name')
            ->select();
        return Res::ok($result);
    }
    
    //继承注册链接
    public function inheritRegisterLink()
    {
        if(!$this->checkPermission()){
            return Res::error('你没有权限操作继承注册链接，请联系管理员！');
        }
        $param = $this->request->param();
        $param['operate_id'] =  $this->request->popuId;
        $result = RegisterLinkService::inheritRegisterLink($param);
        return Res::ok($result);
    }
    
    //获取可以继承的员工列表
    public function getCanInheritStaffList()
    {
        $param = $this->request->param();
        $result = RegisterLinkService::getCanInheritStaffList($param);
        return Res::ok($result);
    }
    
    //获取登录的信息
    public function is_pingtai()
    {
        $result = [
            'is_popu' => $this->request->isPopu
        ];
        return Res::ok($result);
    }
    
    //判断级别是否有权限
    private function checkPermission()
    {
        if($this->request->popuLevel > 0){
            return false;
        }
        return true;
    }
}